/**
 * ReportGear(2011)
 */
package com.reportgear.report.data.datasource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.abbp.report.utils.Dom4jUtils;

import org.dom4j.Document;
import org.dom4j.Element;

import com.reportgear.core.data.SalaryHelper;
import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.report.core.api.idname.DefaultIdName;
import com.reportgear.report.core.api.idname.IdName;

/**
 * JavaScript数据源工厂类
 * 
 * @version 1.0 2011-7-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public enum JsDataSourceFactory {
    INSTANCE;

    /**
     * 数据源Map
     */
    private Map<String, JsDataSource> dsMap = new HashMap<String, JsDataSource>();

    /**
     * 构造函数
     */
    private JsDataSourceFactory() {
        Document doc = Dom4jUtils.createDocumentFromStream(JsDataSourceFactory.class
                .getResourceAsStream("datasource.xml"));

        @SuppressWarnings("unchecked")
        List<Element> datasources = doc.getRootElement().elements("datasource");
        for (Element el : datasources) {
            this.dsMap.put(el.attributeValue("id"), this.createDataSource(el));
        }
    }

    /**
     * 创建数据源
     * 
     * @param el
     *            数据源配置节点信息
     * @return 数据源
     */
    private JsDataSource createDataSource(Element el) {
        JsDataSource ds = new JsDataSource(el.attributeValue("id"), el.attributeValue("caption"));
        ds.setUrl(el.attributeValue("url"));

        @SuppressWarnings("unchecked")
        List<Element> fns = el.elements("field");
        for (Element elf : fns) {
            ds.addField(elf.attributeValue("id"), elf.attributeValue("caption"));
        }

        return ds;
    }

    /**
     * 根据Id获取数据源
     * 
     * @param id
     *            数据源Id
     * @return 数据源
     */
    public JsDataSource getDataSource(String id) {
        return this.dsMap.get(id);
    }

    /**
     * 获取数据源IdName
     * 
     * @return 数据源IdName列表
     */
    public List<IdName<String>> getDataSourceList() {
        List<IdName<String>> result = new ArrayList<IdName<String>>();
        for (JsDataSource ds : dsMap.values()) {
            result.add(new DefaultIdName<String>(ds.getId(), ds.getCaption()));
        }

        return result;
    }

    /**
     * 根据数据源和人员Id查询结果
     * 
     * @param dataSourceId
     *            数据源
     * @param personId
     *            人员Id
     * @return 结果Id
     */
    public List<SalaryDataObject> queryResultResult(String dataSourceId, int personId) {
        JsDataSource jds = this.getDataSource(dataSourceId);

        return SalaryHelper.INSTANCE.getResumeParts(80382, jds.getCaption());
        // List<SalaryDataObject> result = new ArrayList<SalaryDataObject>();
        //
        // return result;
    }

    public static void main(String[] args) {
        JsDataSourceFactory a = JsDataSourceFactory.INSTANCE;
        System.out.println(a.getDataSource("SYS_1"));
    }
}
